package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.demo.common.pojo.dataobject.BlogInfo;
import com.example.demo.common.pojo.request.AddBlogParam;
import com.example.demo.common.pojo.request.UpBlogParam;
import com.example.demo.common.response.BlogInfoResponse;
import com.example.demo.common.utils.BeanConver;
import com.example.demo.mapper.BlogInfoMapper;
import com.example.demo.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogInfoMapper blogMapper;

    /*
    * 获取博客列表*/
    @Override
    public List<BlogInfoResponse> getList() {
        //查询数据库
        List<BlogInfo> blogInfos = blogMapper.selectList(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getDeleteFlag, 0).orderByDesc(BlogInfo::getId));

        //转换(将每一个blogInfos转换成BlogInfoResponse)  将blogInfos转换成流,每个进行处理
//        List<BlogInfoResponse> blogInfoResponses = blogInfos.stream().map(blogInfo -> {
//            BlogInfoResponse blogResponse = new BlogInfoResponse();
//            BeanUtils.copyProperties(blogInfo, blogResponse);
//            return blogResponse;
//        }).collect(Collectors.toList());

        List<BlogInfoResponse> blogInfoResponses = blogInfos.stream()
                .map(blogInfo -> BeanConver.trans(blogInfo)).collect(Collectors.toList());

        return blogInfoResponses;
    }

    @Override
    public BlogInfoResponse getBlogDetail(Integer blogId) {
        BlogInfo blogInfo = selectBlogById(blogId);

        return BeanConver.trans(blogInfo);
    }

    @Override
    public Boolean addBlog(AddBlogParam param) {

        //对象转换 AddBlogParam -> BlogInfo
        BlogInfo blogInfo = BeanConver.trans(param);
        //插入博客
        int result = blogMapper.insert(blogInfo);
        try {
            if(result == 1){
                return true;
            }
        } catch (Exception e) {
            log.error("博客插入失败, e{}", e);
        }
        return false;
    }

    @Override
    public Boolean updateBlog(UpBlogParam param) {
        //对象转换 UpBlogParam -> BlogInfo
        BlogInfo blogInfo = BeanConver.trans(param);
        //更新博客
        return update(blogInfo);
    }

    @Override
    public Boolean deleteBlog(Integer blogId) {

        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        return update(blogInfo);
    }

    //根据BlogId获取博客
    public BlogInfo selectBlogById(Integer blogId){
        return blogMapper.selectOne(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getId, blogId)
                .eq(BlogInfo::getDeleteFlag, 0));
    }

    /**
     * 更新博客数据
     */
    public Boolean update(BlogInfo blogInfo){
        Integer result = blogMapper.updateById(blogInfo);
        try {
            if(result == 1){
                return true;
            }
        } catch (Exception e) {
            log.error("更新博客失败, e:{}", e);
        }
        return false;
    }
}
